<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<%@include file="/quick/include/commonQuery.jsp"%>
<c:set var="buttons">
    <input type="button" style="margin-right: 5px"   onclick="f_add()" value="<fmt:message key="button.add"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_delete()" value="<fmt:message key="button.delete"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_cancel()" value="<fmt:message key="button.cancel"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_save()" value="<fmt:message key="button.save"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_query()" value="<fmt:message key="button.query"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_confirm()" value="<fmt:message key="button.submit"/>"/>
<c:if test='${not empty model.parentId}'>
    <input type="button" style="margin-right: 5px"   onclick="f_back()" value="<fmt:message key="button.back"/>"/>
</c:if>
</c:set>
<div id='toolbar' style="display:none">
<c:out value="${buttons}" escapeXml="false"/>
</div>
<script>
var currentSchoolYear="<c:out value='${model.parameter.schoolYear}'/>";
var currentSchoolSemester="<c:out value='${model.parameter.schoolSemester}'/>";
var scoreType="<c:out value='${model.parameter.scoreType}'/>";
if(scoreType=='01')
  document.title='成绩录入--<fmt:message key="webapp.name"/>';
else if(scoreType=='03')
  document.title='补考成绩录入--<fmt:message key="webapp.name"/>';  
  
Ext.onReady(globalScope._ready);
Ext.onReady(function(){
 var grid=globalScope.grid.get("<c:out value='${model.tableName}'/>");
  var cm=grid.getColumnModel();
  var cols=cm.getColumnsBy(function(c,i){
		if(c.dataIndex=='SCORE'||c.dataIndex=='SCORE2'||c.dataIndex=='SCORE_NATURE'){
			c.header='<b>'+c.header+'</b>';
		}
  });	
  grid.reconfigure(grid.getStore(),cm);
});


<c:if test='${not empty model.parentId}'>
function f_back(){
   var matrix=[['STATUS','N'],['SCORE_TYPE',scoreType],['schoolYear',currentSchoolYear],['schoolSemester',currentSchoolSemester]];
   var form=createForm(g_rootPath+"educational/score/input.html",matrix);
   form.submit();
//   history.back();
}
</c:if>

function f_confirm(){
  var grid=globalScope.grid.get('V_ORIGINAL_SCORE');
  var m=grid.getStore().getModifiedRecords();
  if(m.length){
    alert("确认之前,请先保存修改的成绩!");
    return;
  }
  Ext.Msg.confirm("确认","成绩提交后就不可再修改,并且将公布给学生查询,确定吗?",function(btn){
    if(btn=='yes'){
       Ext.get(document.body).mask('<fmt:message key="info.saveMsg"/>');
	   var id="<c:out value='${model.parentId}'/>";
       Ext.Ajax.request({
         method:'POST',
         url:"<c:url value='/educational/score/confirm.html'/>",
         params:{REFID:id},
         success:function(response){
            Ext.get(document.body).unmask();
			var resp=Ext.decode(response.responseText);
			if(resp.result=='success'){
				Ext.Msg.alert('<fmt:message key="label.info"/>','成绩提交成功',function(){
				  f_back();
				});
			}else{
			   Ext.Msg.alert('<fmt:message key="label.info"/>',resp.errorInfo);
			}
         },
         failure:function(response){
           Ext.get(document.body).unmask();
		   Ext.Msg.alert('<fmt:message key="label.info"/>','<fmt:message key="info.save_error"/>');
         }
      });
    }
  });
}

function f_add(tableName){
  var refid="<c:out value='${model.parentId}'/>";
  f_commonQuery('V_STUDENT',function(recs){
	    Ext.each(recs,function(item){
		      var updtArray=[];
		      var commonQueryGrid=globalScope.grid.get("<c:out value='${model.tableName}'/>");
		      var studentScoreStore=commonQueryGrid.getStore();
			  var newRecord=commonQueryGrid.newRecord('newId');//新建Record
			  newRecord.copyData(item.data); //复制数据
			   newRecord.set('SCORE','');
			   newRecord.set('SCORE2','');
              newRecord.set('SCORE_NATURE','S');//成绩类型,正常考试成绩
 			  newRecord.set('SCORE_TYPE','01');//成绩类型,正常考试成绩
			  commonQueryGrid.stopEditing();//插入Record
	    	  studentScoreStore.insert(0,newRecord); 
	    	  commonQueryGrid.startEditing(0,0);
	    	  newRecord.dirty=true;
	    	  newRecord.modified=newRecord.data;
	    	  if(studentScoreStore.modified.indexOf(newRecord)==-1){
	    	    studentScoreStore.modified.push(newRecord);	
	    	  }	
	    });	
  },
	  function(params){
        Ext.apply(params,{commonQueryCondition:'student_score_conditon',placeValues:"['"+refid+"']"});	  
	  }); 
}

function f_update(tableName){
  globalScope._update(tableName);
}

function f_delete(tableName){
  globalScope._delete(tableName);
} 

function f_cancel(tableName){
  globalScope._cancel(tableName);
}

function f_save(tableName,url){
  var refid="<c:out value='${model.parentId}'/>";
  var grid=globalScope.grid.get('V_ORIGINAL_SCORE');
  var m=grid.getStore().getModifiedRecords(); 
  var recs=[];
  for(var i=0;i<m.length;i++){
    var score1=m[i].get('SCORE');
    var score2=m[i].get('SCORE2');
    if(score1!=score2){
      var sname=m[i].get('STUDENT_NAME');
      recs.push(m[i]);
    }
  }
  if(recs.length>0){
    grid.getSelectionModel().selectRecords(recs);
    Ext.Msg.alert('错误',"选中的记录,输入的两次成绩不一致!");
    return;
  }
  globalScope._simpleSave({tableName:'V_ORIGINAL_SCORE',saveTableName:'T_ORIGINAL_SCORE',url:'quick/quickJsonProcess.html',postData:{'REFID':refid}});
}

function f_query(tableName){
    globalScope._query(tableName);
}
function f_gotoPage(pageNo,rowsOfPage,pageCount,rowCount,conditoin){
  globalScope._gotoPage({spanName:"g_pageSpan_<c:out value='${model.tableName}'/>",pageNo:pageNo,rowsOfPage:rowsOfPage,pageCount:pageCount,rowCount:rowCount,condition:conditoin,tableName:"<c:out value='${model.tableName}'/>"});
}
</script>
